package com.atguigu.guli.service.vod.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.utils.StringUtils;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.vod.service.MediaService;
import com.atguigu.guli.service.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
@Slf4j
public class MediaServiceImpl implements MediaService {
    @Autowired
    private VodProperties vodProperties;



    @Override
    public String uploadVideo(InputStream is, String originalFileName) {
        String title = originalFileName.substring(0,originalFileName.lastIndexOf("."));
        UploadStreamRequest request = new UploadStreamRequest(vodProperties.getAccesskeyId(), vodProperties.getAccesskeySecret(), title, originalFileName, is);
        request.setWorkflowId(vodProperties.getWorkflowId());
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        String videoId = response.getVideoId();
        if(StringUtils.isEmpty(videoId)){
            log.error("阿里云上传失败：" + response.getCode() + " - " + response.getMessage());
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }

        return videoId;
    }

    @Override
    public String getPlayUrl(String videoId) {
        DefaultAcsClient client = initVodClient(vodProperties.getAccesskeyId(), vodProperties.getAccesskeySecret());
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            /*获取播放地址函数*/
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(videoId);
            response =  client.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            String playURL = playInfoList.get(0).getPlayURL();
            return playURL;

        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.FETCH_PLAYURL_ERROR,e);
        }

    }

    @Override
    public String getPlayAuth(String videoId) {
        DefaultAcsClient client = initVodClient(vodProperties.getAccesskeyId(), vodProperties.getAccesskeySecret());
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            request.setAuthInfoTimeout(3000L);
            response =  client.getAcsResponse(request);
            return response.getPlayAuth();

        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.FETCH_PLAYAUTH_ERROR,e);
        }

    }

    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
}
